﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using TA.Turbo.Design;
using TA.DotNetUtils;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Definition of a custom server-side event which can triggered on the client.
	/// </summary>
	public class PageEvent : BaseSpecialControl, IAssignedClientId
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public PageEvent()
		{
		}
		#endregion
		#region EVENTS
		public event ParameterEventHandler<string> Server;
		#endregion
		#region PROPS
		public override string UniqueID
		{
			get
			{
				return TPage.GetOptimizedUniqueId(ref this._uniqueId, base.UniqueID, true, 'E');
			}
		}
		/// <summary>
		/// The ID of the post scenario to use when raising the event.
		/// </summary>
		[DefaultValue("")]
		[TypeConverter(typeof(ControlIdConverter<PostScenarioIdLister>))]
		[IDReferenceProperty]
		[NotifyParentProperty(true)]
		public string PostScenarioId
		{
			get
			{
				return _postScenarioId;
			}
			set
			{
				_postScenarioId = value;
				_postScenario = null;
			}
		}
		/// <summary>
		/// The post scenario to use when raising the event.
		/// </summary>
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public PostScenario PostScenario
		{
			get
			{
				if (_postScenario == null)
				{
					if (!string.IsNullOrEmpty(this.PostScenarioId))
					{
						this._postScenario = this.FindControlGlobal(this.PostScenarioId) as PostScenario;
						if (this._postScenario == null)
							throw new InvalidOperationException(string.Format("The PostScenario with the Id '{0}' wasn't found.", this.PostScenarioId));
					}
				}
				return this._postScenario;
			}
			set
			{
				this._postScenarioId = value.UniqueID;
				this._postScenario = value;
			}
		}
		/// <summary>
		/// Gets/sets the client side variable name used to store the unique ID of this event.
		/// </summary>
		[DefaultValue("")]
		[NotifyParentProperty(true)]
		public string Variable
		{
			get
			{
				if(string.IsNullOrEmpty(this._variable))
					return this.ID;

				return this._variable;
			}
			set
			{
				this._variable = value;
			}
		}
		#endregion
		#region METHODS
		/// <summary>
		/// Raises this event with the specified argument as the parameter.
		/// </summary>
		/// <param name="argument"></param>
		public virtual void Raise(string argument)
		{
			if (this.Server != null)
				this.Server(TPage.Current, new ParameterEventArgs<string>(argument));
		}
		public virtual string GetScript()
		{
			return string.Format("{0} = \"{1}\";", this.GetVariableName(), this.UniqueID);
		}
		#endregion
		#endregion
		#region INTERNAL
		#region CONST/STATIC
		#endregion
		#region VARS
		string _postScenarioId;
		PostScenario _postScenario;
		string _uniqueId;
		string _variable = "";
		#endregion
		#region PROPS
		#endregion
		#region METHODS
		#region INIT
		#endregion
		#region DATA
		/// <summary>
		/// Returns the complete variable name to which this event's ID needs to be assigned.
		/// </summary>
		/// <returns></returns>
		protected string GetVariableName()
		{
			Manager m = this.Parent as Manager;
			if (m != null)
				return m.ClientScriptVariablePrefix + this.Variable;
			return this.Variable;
		}
		#endregion
		#region HANDLERS
		#endregion
		#endregion
		#endregion
		#region HIDDEN
		[EditorBrowsable(EditorBrowsableState.Never)]
		[Browsable(false)]
		public new event EventHandler Init;
		[EditorBrowsable(EditorBrowsableState.Never)]
		[Browsable(false)]
		public new event EventHandler Disposed;
		#endregion

	}
}
